//
//  XMClassViewController.m
//  Second
//
//  Created by Alex on 16/7/13.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "XMClassController.h"

@interface XMClassController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@end

@implementation XMClassController

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc]init];
        //设置item的大小
        layOut.itemSize     = CGSizeMake(kWidth/2-10, 50);
        //设置分区上下左右距离
        layOut.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
        //区头大小
        layOut.headerReferenceSize = CGSizeMake(kWidth, 30);
        _collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layOut];
        _collectionView.delegate   = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor grayColor];
        
    }
    return _collectionView;
}
//分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 2;
}
//每个分区item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 6;
    }else{
        
        return 4;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor grayColor];
    self.navigationItem.title = @"分类";
    
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"class_cell"];
    //注册区头
    [self.collectionView registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"Header"];
    //注册自定义的cell
    [self.collectionView registerNib:[UINib nibWithNibName:@"ClassCell" bundle:nil] forCellWithReuseIdentifier:@"class_cell"];
    
}



//cell的样式
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ClassCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"class_cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor lightGrayColor];
    NSArray *titleArray = @[@"手机",@"数码",@"运动器材",@"服饰",@"图书教材",@"代步工具"];
    NSArray *listArray = @[@"苹果 华为",@"电脑 相机",@"篮球 哑铃",@"衣服 鞋子",@"课本 笔记",@"自行车 电动车"];
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%200/255.0 blue:arc4random()%255/255.0 alpha:1];
    
    if (indexPath.section == 0) {
        
        [cell setCellimageName:@"2" title:titleArray[indexPath.row] list:listArray[indexPath.row]];
        
        return cell;
    }
    
    
    if (indexPath.section == 1 ) {
        
        if (indexPath.row == 0) {
            
            [cell setCellimageName:@"26.jpg" title:@"代跑腿" list:@"取快递 送外卖"];
        }
        if (indexPath.row == 1) {
            
            [cell setCellimageName:@"26.jpg" title:@"辅导课程" list:@"期末冲关 考研"];
        }
        if (indexPath.row == 2) {
            
            [cell setCellimageName:@"26.jpg" title:@"陪玩" list:@"打游戏 打球"];
        }
        return cell;
    }
    return nil;
}

//返回区头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    HeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
    //设置区头的类容
    if (indexPath.section == 0) {
        
        headerView.label.text = @"二手市场";
        
    }else{
        
        headerView.label.text = @"一技之长";
    }
    
    return headerView;
}
//点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    XMClassListViewController *classListVC = [[XMClassListViewController alloc]init];
    
    NSArray *array = @[@2,@4,@8,@6,@7,@1];
    if (indexPath.section == 0) {
        
        classListVC.typeID = array[indexPath.row];
   
    }
    
    
    [self.navigationController pushViewController:classListVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}


@end
